<template>
<div>
  <el-dialog
    :title="title"
    size="small"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="流程名称" prop="processName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.processName" placeholder="请填写流程名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="创建人" prop="enterpriseUser.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择创建人"
            labelName = 'userName'
            labelValue = 'id'
            :value = "inputForm.enterpriseUser.id"
            :limit="1"
            @getValue='(value) => {inputForm.enterpriseUser.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'userName',
              label: '用户名'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'userName',
              label: '用户名'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/list"
            entityBeanName="enterpriseUserInformation"
            queryEntityUrl="/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="公司名称" prop="companyName.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择公司名称"
            labelName = 'companyName'
            labelValue = 'id'
            :value = "inputForm.companyName.id"
            :limit="1"
            @getValue='(value) => {inputForm.companyName.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/list"
            entityBeanName="enterpriseInformation"
            queryEntityUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="时间" prop="processTime"
                :rules="[
                 ]">
                <el-date-picker
                      v-model="inputForm.processTime"
                      type="datetime"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                  {required: true, message:'备注信息不能为空', trigger:'blur'}
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- (0:请假 1:外出 2:出差 3:加班 4:报销 5:其他 process_mtype) -->
            <el-form-item label="流程类型" prop="processMaintype"
                :rules="[
                 ]">
                <el-select v-model="inputForm.processMaintype" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('process_mtype')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
    <el-col :span="24">
     <el-form-item label-width="0">
            <el-tabs v-model="processMainTab">
            <el-tab-pane label="流程附表">
                  <el-button size="small" @click="addProcessAttachRow" type="primary">新增</el-button>
                  <el-table
                  class="table"
                  size="small"
                  :data="inputForm.processAttachList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
                  <el-table-column
                    prop="processName.processName"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="流程">
                  </el-table-column>
                <el-table-column
                    prop="number"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="序号">
                  </el-table-column>
                  <el-table-column
                    prop="attachRole.roleName"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="审批角色">
                  </el-table-column>
                <el-table-column
                    prop="attachType"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="审批类型">
                    <template slot-scope="scope">
                        {{ $dictUtils.getDictLabel("attachtype", scope.row.attachType, '-') }}
                    </template>
                  </el-table-column>
                <el-table-column
                    prop="parallelNumber"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="并行序号">
                  </el-table-column>
                <el-table-column
                    prop="failNumber"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="失败返回序号">
                  </el-table-column>
                <el-table-column
                    prop="remarks"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="备注信息">
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewProcessAttachRow(scope.row)" type="text" size="mini">查看</el-button>
                      <el-button  @click="editProcessAttachRow(scope.row)" type="text" size="mini">编辑</el-button>
                      <el-button  @click="delProcessAttachRow(scope.row)" type="text" size="mini">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
           </el-form-item>
          </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
  <ProcessAttachForm ref="processAttachForm" @addRow="saveProcessAttachRow(arguments)"></ProcessAttachForm>
</div>
</template>

<script>
  import ProcessAttachForm from './ProcessAttachForm'
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        processMainTab: '0',
        inputForm: {
          id: '',
          processAttachList: [],
          processName: '',
          enterpriseUser: {
            id: ''
          },
          companyName: {
            id: ''
          },
          processTime: '',
          remarks: '',
          processMaintype: ''
        }
      }
    },
    components: {
      GridSelect,
      ProcessAttachForm
    },
    methods: {
      init (method, id) {
        this.method = method
        if (method === 'add') {
          this.title = `新建流程主附表`
        } else if (method === 'edit') {
          this.title = '修改流程主附表'
        } else if (method === 'view') {
          this.title = '查看流程主附表'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = id
          this.processMainTab = '0'
          this.inputForm.processAttachList = []
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/processmain/processMain/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.processMain)
              this.loading = false
            })
          }
        })
      },
      saveProcessAttachRow (child) {
        if (child[0] === '') {
          this.inputForm.processAttachList.push(child[1])
        } else {
          this.inputForm.processAttachList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.processAttachList.splice(index, 1, child[1])
            }
          })
        }
      },
      addProcessAttachRow (child) {
        this.$refs.processAttachForm.init('add')
      },
      viewProcessAttachRow (child) {
        this.$refs.processAttachForm.init('view', child)
      },
      editProcessAttachRow (child) {
        this.$refs.processAttachForm.init('edit', child)
      },
      delProcessAttachRow (child) {
        this.inputForm.processAttachList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.processAttachList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.processAttachList.splice(index, 1, item)
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/processmain/processMain/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>